package com.platform.manager.action.system;

import com.platform.domain.TbSysRole;
import com.platform.domain.result.TreeGridData;
import com.platform.manager.action.BaseAction;
import com.platform.service.RoleService;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wangyanginfo
 * Date: 12-9-18
 * Time: 下午2:31
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/system")
@Service("roleAction")
@ParentPackage("json-default")
public class RoleAction extends BaseAction {

    @Resource
    private RoleService roleService;

    private List<TbSysRole> roles;

    private List<TbSysRole> notRoles;

    private String userId;

    /**
     * 查询用户角色权限列表
     *
     * @return
     */
    @Action(value = "queryUserRoles", results = {@Result(name = "success", type = "json", params = {"root", "datas"})})
    public String queryUserRoles() {
        try {
            if (userId != null) {
                logger.info(userId);
            }
            // 查询符合条件的记录总数
            roles = roleService.queryRoleByUserId(userId);

            if (roles != null) {
                datas = new ArrayList<TreeGridData>();
                for (TbSysRole role : roles) {
                    TreeGridData data = new TreeGridData();
                    data.setId(role.getRoleId());
                    data.setText(role.getRoleName());
                    data.setState("closed");
                    datas.add(data);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询用户角色权限列表
     *
     * @return
     */
    @Action(value = "editUserRoles", results = {@Result(name = "success", location = "/system/user/user_role.jsp")})
    public String editUserRoles() {
        try {
            // 已经授权的角色集合
            roles = roleService.queryRoleByUserId(userId);
            // 未授权的角色集合
            notRoles = roleService.queryRoleByNotUserId(userId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询用户角色权限列表
     *
     * @return
     */
    @Action(value = "queryByUserRoles", results = {@Result(name = "success", type = "json", params = {"root", "roles"})})
    public String queryByUserRoles() {
        try {
            // 已经授权的角色集合
            roles = roleService.queryRoleByUserId(userId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 查询用户角色权限列表
     *
     * @return
     */
    @Action(value = "queryByNotUserRoles", results = {@Result(name = "success", type = "json", params = {"root", "notRoles"})})
    public String queryByNotUserRoles() {
        try {
            // 未授权的角色集合
            notRoles = roleService.queryRoleByNotUserId(userId);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return SUCCESS;
    }

    @JSON
    public List<TbSysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<TbSysRole> roles) {
        this.roles = roles;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public List<TbSysRole> getNotRoles() {
        return notRoles;
    }

    public void setNotRoles(List<TbSysRole> notRoles) {
        this.notRoles = notRoles;
    }
}
